package com.zcc.makingfriend.controller;

import com.zcc.makingfriend.service.AttachmentService;
import com.zcc.makingfriend.untils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author zcc
 */
@Api(tags = "文件上传")
@RestController
@RequestMapping("/attachment")
public class AttachmentController {
    @Autowired
    private AttachmentService attachmentService;

    @ApiOperation("上传文件")
    @PostMapping("/upload")
    public Result<List<String>> upload(MultipartFile[] files) {
        System.out.println(files);
        return Result.success(200, "上传成功", attachmentService.upload(files));
    }

    @ApiOperation("下载文件")
    @GetMapping("/download")
    public Result<?> download(HttpServletResponse resp, String objectName) {
        int code = attachmentService.download(resp, objectName);
        if (code == 200) {
            return Result.success("下载成功");
        }
        return Result.fail("下载失败");
    }

}
